#ifndef _MAZE_EXPLORER
#define _MAZE_EXPLORER

// Make sure the correct oscillator speed is set
#undef HSE_Value
#define HSE_Value ((u32)12000000)

#include "circle_api.h"
#include "stm32f10x_conf.h"
#include "create_driver.h"
#include "ping_driver.h"
#include "usart_buffer.h"

// The following should be the minimal CircleOS version needed by your application
#define NEEDEDVERSION "V 3.7"

#define EMERGENCY_STOP_VALUE 10
#define GPIOPing    GPIOB
#define FRONT_PING  GPIO_Pin_15
#define RF_PING     GPIO_Pin_12
#define RR_PING     GPIO_Pin_13

// maze exploring states
enum eMazeExplorerStates { 
    STATE_IDLE, 
    STATE_MOVE_FORWARD, 
    STATE_ROTATE_CCW, 
    STATE_ROTATE_CW, 
    STATE_STOPPED, 
    STATE_STUCK };

// orientations for maze exploring
enum eOrientation { NORTH, EAST, SOUTH, WEST };

#define FRONT_OBSTACLE_DIST     35
#define SIDE_OBSTACLE_DIST      30
#define FRONT_NO_OBSTACLE_DIST  115
#define SIDE_NO_OBSTACLE_DIST   100
#define CELL_DISTANCE           686 // 2'4" = 711.2mm


typedef struct
{
    u16 frontPing;
    u16 rfPing;
    u16 rrPing;
    u16 frontPingRaw;
    u16 rfPingRaw;
    u16 rrPingRaw;
    s16 leftVel;
    s16 rightVel;
    s32 distance;
    s32 angle;
    u8 bumpers;
} GLOBALS;


/* Function Prototypes */
void USART1_Init( void );
void TIM1_Init( void );
void CREATE_Init( void );
void GLOBALS_Init( void );
void NVIC_Configuration(void);
tHandler TIM1_Irq( void );
void updateStatus( void );
void updateSensors( void );
u32 getTimeMicro( void );
void alignWithWall( void );
void followTheWall( void );
void followTheWall_grid( void );
void AccelerometerMovement( void );
void JoystickMovement( void );
s16 goForward(s16 distance, u16 speed);
s16 rotateCW(s16 angle, u16 speed);
s16 rotateCCW(s16 angle, u16 speed);
u16 clampDegrees(s16 angle);
u16 makeRightAngle(s16 angle);


#endif //_MAZE_EXPLORER
